//
//  TBCAppTools.m
//  待定
//
//  Created by BoyaLee on 15/11/5.
//  Copyright © 2015年 BoyaLee. All rights reserved.
//

#import "TBCAppTools.h"

@implementation TBCAppTools

+ (instancetype)shareBLDAppTools {
    
    static TBCAppTools *appTools = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        appTools = [[TBCAppTools alloc] init];
    });
    
    return appTools;
    
}

#pragma mark -- 强制屏幕水平
+ (void)changeScreenHorizontal {
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = UIInterfaceOrientationLandscapeRight;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
        
    }
    
}

#pragma mark -- 强制屏幕垂直
+ (void)changeScreenVertical {
    
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
        
    }
    
}

#pragma mark -- GET请求AFN网络数据的方法
+ (void)getLocalDataWithURL:(NSString *)url TBCToolsBlock:(TBCToolsBlock)TBCToolsBlock {
    
    NSString *url_string = [NSString stringWithFormat:@"%@",url];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",@"application/x-javascript", nil];
    
    [manager GET:url_string parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        TBCToolsBlock(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
}

#pragma mark -- POST请求AFN网络数据的方法
+ (void)postLocalDataWithURL:(NSString *)url Parameters:(NSDictionary *)parameters Cookie:(NSString *)cookie TBCToolsBlock:(TBCToolsBlock)TBCToolsBlock {
    
    NSString *url_string = [NSString stringWithFormat:@"%@",url];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",@"application/x-javascript", nil];
    [manager POST:url_string parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        TBCToolsBlock(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
}

+ (void)getDatawithPost:(NSString *)url paramenters:(NSDictionary *)para cookie:(NSString *)cookie Block:(TBCToolsBlock)TBCToolsBlock
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"cookie"];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",@"application/x-javascript", nil];
    
    [manager POST:url parameters:para success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        TBCToolsBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
        NSLog(@"失败==== %@", error);
    }];
}

#pragma mark -- 判断是否有网
+ (NSInteger)getInternetReachability:(NSString *)url {
    
    Reachability *reachability = [Reachability reachabilityWithHostName:url];
    
    switch ([reachability currentReachabilityStatus]) {
            
        case ReachableViaWiFi:
            NSLog(@"使用WiFi网络");
            return InternetReachableWithWiFi;
            
            break;
            
        case ReachableViaWWAN:
            NSLog(@"使用数据连接");
            return InternetReachableWithWWAN;
            
            break;
            
        default:
            NSLog(@"没有网络连接");
            return InternetReachableWithNone;
            
            break;
            
    }
    
}

@end
